﻿using ATS.UI;
using Microsoft.Extensions.Logging;

namespace ATS.Plugins.Template.ViewModels;

public class MainViewModel : ViewModelBase
{
    public MainViewModel()
    {
        OkCommand = new AsyncRelayCommand(DoWork);
    }

    private string _title = "插件模板";

    public string Title { get => _title; set => SetProperty(ref _title, value); }

    public AsyncRelayCommand OkCommand { get; }

    private async Task DoWork()
    {
        //!!! AsyncRelayCommand 会在外部捕获异常，若无必要这里不需要 try-catch

        Logger.LogInformation("开始执行任务...");

        #region ViewModelBase继承的方法
        // 仅用于演示

        // 检查权限
        if (HasPermission(Permissions.Default.MenuPluginsTemplate))
        {

        }

        // 检查角色
        if (HasRole("admin"))
        {

        }

        #endregion

        await Task.Delay(1000);

        Logger.LogInformation("任务执行完成");   
        
        Notification.Info("任务执行完成"); // 提示
    }


    #region INavigationAware

    /// <summary>
    /// 进入页面后加载初始数据
    /// </summary>
    /// <returns></returns>
    protected override async Task OnLoadingAsync(NavigationContext navigationContext)
    {
        // 获取配置
        var settingValue = await GetSettingValueAsync(SettingConfiguration.TemplateValue);
        this.Title = settingValue!;
    }

    public override bool IsNavigationTarget(NavigationContext navigationContext)
    {
        // 进入页面时触发       
        return true;  // 返回 true 表示该页面可以进入
    }

    public override void OnNavigatedFrom(NavigationContext navigationContext)
    {
        // 离开页面时触发
    }

    #endregion

}
